package com.tjsinfo.tjsoa.articles.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.articles.bo.ISummaryBO;
import com.tjsinfo.tjsoa.articles.bo.ManageSuperAction;
import com.tjsinfo.tjsoa.articles.vo.TRules;
import com.tjsinfo.tjsoa.articles.vo.TSummaryInfo;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.vo.User;

public class SummaryAction extends ManageSuperAction{
	private ISummaryBO summaryBO;

	public ISummaryBO getSummaryBO() {
		return summaryBO;
	}

	public void setSummaryBO(ISummaryBO summaryBO) {
		this.summaryBO = summaryBO;
	}
	
	/**
	 * 工作总结主界面
	 * @return
	 */
	public ActionForward summaryMain(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		String year	= "";
		if (request.getParameter("year") != null) {
			year = request.getParameter("year");
			if(Integer.parseInt(year) <2018){
				year = "2018";
			}
		}else{
			year = "2018";
		}
		List<String> yearmonthstr = new ArrayList<String>();
		List<String> yearhatestr = new ArrayList<String>();
		List<Integer> yearList = new ArrayList<Integer>();
		for(Integer month = 1;month<=12;month++){
			yearmonthstr.add(year+"-"+month);
		}
		for(Integer yearInt = 2018;yearInt<=2029;yearInt++){
			yearhatestr.add(yearInt+"-上半年");
			yearhatestr.add(yearInt+"-下半年");
		}	
		for(Integer yearInt = 2018;yearInt<=2029;yearInt++){
			yearList.add(yearInt);
		}		
		request.setAttribute("year",Integer.parseInt(year));
		request.setAttribute("yearmonthstr", yearmonthstr);
		request.setAttribute("yearhatestr", yearhatestr);		
		request.setAttribute("yearList", yearList);
		return mapping.findForward("summaryMain");
	}
	
	
	/**
	 * 月度工作总结部门列表
	 * @return
	 */
	public ActionForward departmentSummary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		String yearmonthstr = request.getParameter("yearmonthstr");
		request.setAttribute("yearmonthstr", yearmonthstr);
		return mapping.findForward("departmentSummary");
	}
	
	/**
	 * 月度工作总结部门列表AJAX
	 * @return
	 */
	public ActionForward departmentSummaryList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		String yearmonthstr = request.getParameter("yearmonthstr");
		List<Map<String, String>> list = summaryBO.selectDepartmentSummary(SessionUser.getUserId(request),yearmonthstr);
		JSONArray array	 = new JSONArray();
		for (Map<String, String> map : list) {
			JSONObject jsonObject = new JSONObject(map);
			array.put(jsonObject);
		}
		response.getWriter().print(array);
		return null;
	}
	/**
	 * 工作总结主界面
	 * @return
	 */
	public ActionForward toAddSummary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		String yearmonthstr = request.getParameter("yearmonthstr");
		String depmentId = request.getParameter("depmentId");
		request.setAttribute("yearmonthstr", yearmonthstr);
		request.setAttribute("depmentId", depmentId);
		return mapping.findForward("addSummary");
	}
	
	/**
	 * 添加工作总结
	 * @return
	 */
	public ActionForward addSummary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		String yearmonthstr = request.getParameter("yearmonthstr");
		String depmentId = request.getParameter("depmentId");
		String info = request.getParameter("info");
		TSummaryInfo summaryInfo = new TSummaryInfo();
		summaryInfo.setCreatedate(new Date());
		summaryInfo.setCreateDepartmentId(depmentId);
		summaryInfo.setCreateUserId(SessionUser.getUserId(request));
		summaryInfo.setInfo(info);
		summaryInfo.setInfodate(yearmonthstr);
		if (summaryBO.addSummary(summaryInfo) > 0 ) {
			if (summaryInfo.getInfodate().length() == 4) {
				request.setAttribute("type", "month");
			} else {
				request.setAttribute("type", "year");	
			}
			request.setAttribute("summaryInfo", summaryInfo);
			request.setAttribute("message", "save");
			return mapping.findForward(SHOWMESSAGE);
		}
		return null;
	}
	
	/**
	 * 工作总结主界面
	 * @return
	 */
	public ActionForward toUpdateSummary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		Integer summaryid =  Integer.parseInt(request.getParameter("summaryid"));
		TSummaryInfo summaryInfo = summaryBO.getSummaryId(summaryid);
		request.setAttribute("summaryInfo", summaryInfo);
		return mapping.findForward("updateSummary");
	}
	
	/**
	 * 工作总结主界面
	 * @return
	 */
	public ActionForward updateSummary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		Integer summaryid =  Integer.parseInt(request.getParameter("summaryid"));
		String  info = request.getParameter("info");
		TSummaryInfo summaryInfo = summaryBO.getSummaryId(summaryid);
		summaryInfo.setInfo(info);
		summaryInfo.setCreatedate(new Date());
		summaryBO.updateSummary(summaryInfo);
		if (summaryInfo.getInfodate().length() == 4) {
			request.setAttribute("type", "month");
		} else {
			request.setAttribute("type", "year");	
		}
		request.setAttribute("summaryInfo", summaryInfo);
		request.setAttribute("message", "update");
		return mapping.findForward(SHOWMESSAGE);
	}
	
	/**
	 * 工作总结主界面
	 * @return
	 */
	public ActionForward viewSummary(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		Integer summaryid =  Integer.parseInt(request.getParameter("summaryid"));
		TSummaryInfo summaryInfo = summaryBO.getSummaryId(summaryid);
		request.setAttribute("summaryInfo", summaryInfo);
		return mapping.findForward("viewSummary");
	}
	
	
	
}
